var users = new Array();

var operateType;

function user(code, userName, sex, passWord, age, birthday) {

    this.code = code;
    this.userName = userName;
    this.sex = sex;
    this.passWord = passWord;
    this.age = age;
    this.birthday = birthday;
}

function init() {

    users[0] = new user("1001", "张三", "女", "1234", "29", "1991-1-1");
    users[1] = new user("1002", "李四", "男", "1234", "28", "1991-1-1");
    users[2] = new user("1003", "王五", "女", "1234", "27", "1991-1-1");
    users[3] = new user("1004", "赵六", "女", "1234", "26", "1991-1-1");

    refresh(users);
}

function refresh(users) {

    var tbody = document.getElementById("tbody");
    var html = "";
    var color = "warning";
    var flag = true;
    for (var i in users) {

        if (flag) {

            color = "info";
        } else {

            color = "warning";
        }

        html = html + "<tr class='" + color + "'>" +
            "<td style='width:30px;'><input type='checkbox'></td>" +
            "<td id='code'>" + users[i].code + "</td>" +
            "<td id='userName'>" + users[i].userName + "</td>" +
            "<td id ='sex'>" + users[i].sex + "</td>" +
            "<td id='passWord'>" + users[i].passWord + "</td>" +
            "<td id='age'>" + users[i].age + "</td>" +
            "<td id='birthday'>" + users[i].birthday + "</td>" +
            "</tr>";
        flag = !flag;
    }

    tbody.innerHTML = html;
}

$("#user_add").on("click", function () {

    operateType = "add";
});

$("#user_edit").on("click", function () {

    operateType = "edit";
});

$("#save").on("click", function () {

    if (operateType == "add") {

        var newUser = new user(
            $("#m_code").val(),
            $("#m_userName").val(),
            $("#m_sex").val(),
            $("#m_passWord").val(),
            $("#m_age").val(),
            $("#m_birthday").val()
        );
        users = users.concat(newUser);
    } else if (operateType == "edit") {

        for (var i = 0; i < users.length; i++) {

            if (code == users[i].code) {

                users[i].code = $("#m_code").val();
                users[i].userName = $("#m_userName").val();
                users[i].sex = $("#m_sex").val();
                users[i].passWord = $("#m_passWord").val();
                users[i].age = $("#m_age").val();
                users[i].birthday = $("#m_birthday").val();
            }

        }

        alert("修改成功");
    }

    refresh(users);
});

$("#user_delete").on("click", function () {

    console.log(1);
    var checkboxs = $("input[type='checkbox']");
    let f = false;
    for (var i = 0; i < checkboxs.length; i++) {

        var box = checkboxs[i];
        if (box.checked) {

            f = true;
            box = $(box);
            let code = box.parent().siblings("#code").text();
            for (var i = 0; i < users.length; i++) {

                if (code == users[i].code) {

                    users.splice(i, 1);
                    alert("删除成功!");
                }
            }
        }
    }

    if (!f) {

        alert("请选择一个用户！");
    }

    refresh(users);
});

var code;
$("#user_edit").on("click", function () {

    var checkboxs = $("input[type='checkbox']");
    let f = false;
    for (var i = 0; i < checkboxs.length; i++) {

        var box = checkboxs[i];
        if (box.checked) {

            f = true;
            $(this).attr("data-toggle", "modal");
            $(this).attr("data-target", "#myModal");
            box = $(box);
            code = box.parent().siblings("#code").text();
            var userName = box.parent().siblings("#userNmae").text();
            var sex = box.parent().siblings("#sex").text();
            var passWord = box.parent().siblings("#passWord").text();
            var age = box.parent().siblings("#age").text();
            var birthday = box.parent().siblings("#birthday").text();

            $("#m_code").val(code);
            $("#m_userName").val(userName);
            $("#m_sex").val(sex);
            $("#m_passWord").val(passWord);
            $("#m_age").val(age);
            $("#m_birthday").val(birthday);
        }
    }

    if (!f) {

        alert("请选择一个用户！");
        $(this).attr("data-toggle", null);
        $(this).attr("data-target", null);
    }

});

var newusers;
$("#user_find").on("click", function () {

    newusers = new Array();
    var codeLike = $(this).siblings("#s_code").val();
    for (var i = 0; i < users.length; i++) {

        if (users[i].code.indexOf(codeLike) >= 0) {

            newusers = newusers.concat(users[i]);
        }
    }

    refresh(newusers);
});